//
//  BaseViewController.swift
//  CWLiving
//
//  Created by 方正刚 on 2017/7/13.
//  Copyright © 2017年 方正刚. All rights reserved.
//

import UIKit

private let ITEM_MARGIN: CGFloat = 10
let ITEM_WDITH = (SCREEN_WIDHT - 3 * ITEM_MARGIN) / 2
let NORMOLITEM_HEIGHT = ITEM_WDITH * 3 / 4
let PRETYITEM_HEIGHT = ITEM_WDITH * 4 / 3
private let HEADE_HEIGHT: CGFloat = 50

private let NORMOL_CELL = "NORMOL_CELL"
private let HEADE_VIEW = "HEADE_VIEW"
let PRETY_CELL = "PRETY_CELL"

class BaseAnchorViewController: BaseViewController {

    
    var baseVM: BaseViewModel!
    
    /// 懒加载内容属性
    lazy var collectionView: UICollectionView = { [unowned self] in
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width: ITEM_WDITH, height: NORMOLITEM_HEIGHT)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = ITEM_MARGIN
        layout.sectionInset = UIEdgeInsetsMake(0, ITEM_MARGIN, 0, ITEM_MARGIN)
        
        let collectionV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        layout.headerReferenceSize = CGSize(width: SCREEN_WIDHT, height: HEADE_HEIGHT)
        
        collectionV.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        
        collectionV.backgroundColor = UIColor.white
        
        collectionV.dataSource = self
        collectionV.delegate = self as? UICollectionViewDelegate
        collectionV.register(UINib(nibName: "CollectionNormalCell",bundle: nil), forCellWithReuseIdentifier: NORMOL_CELL)
        collectionV.register(UINib(nibName: "CollectionPrettyCell",bundle: nil), forCellWithReuseIdentifier: PRETY_CELL)
        collectionV.register(UINib(nibName: "RecommendHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HEADE_VIEW)
        return collectionV
        }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        loadData()
        
    }

    
}

// MARK: - 设置UI
extension BaseAnchorViewController {
    override func setupUI() {
        
        contentView = collectionView
        view.addSubview(collectionView)
        super.setupUI()
    }
}


// MARK: - 加载数据
extension BaseAnchorViewController {
    func loadData() {
        
       
    }
}

// MARK: - UICollectionViewDataSource 数据源协议
extension BaseAnchorViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return baseVM.anchorGroups.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let group = baseVM.anchorGroups[section]
        
        return group.room_listModels.count
        
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let group = baseVM.anchorGroups[indexPath.section]
        
        let anchor = group.room_listModels[indexPath.item]
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NORMOL_CELL, for: indexPath) as! CollectionNormalCell
        
        cell.achor =  anchor
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let headerV = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HEADE_VIEW, for: indexPath) as! RecommendHeaderView
        
        headerV.group = baseVM.anchorGroups[indexPath.section]
        
        return headerV
    }
}

